﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using  CaiyinSoft.NetGather.Protocol;


namespace CaiyinSoft.NetGather.NetGatherLib
{
    [Serializable]
    public class URLConverter
    {
        private string _InputURL;
        public void SetInputURL(string url)
        {
            _InputURL = url;
        }
        public string GetInputURL()
        {
            return _InputURL;
        }

        //public string InputURL
        //{
        //    get;
        //    set;
        //}

        public URLConverter()
        {
            Encoding = "UTF-8";// "GBK"; "gb2312";
            Interval = 60;
        }
        /// <summary>
        /// 获取或设置抓取的是不是列表数据
        /// </summary>
        public bool ListResult { get; set; }

        public void ReInit()
        {
            foreach (var filter in this.HTMLAnalyzer.HTMLFilters)
            {
                enabledFilter(filter);
            }
        }


        private void enabledFilter(IHTMLFilter filter)
        {
            filter.Runabled = true;
            foreach (var f in filter.ChildFilters)
            {
                enabledFilter(f);
            }
        }

        /// <summary>
        /// 时间间隔.(秒)
        /// </summary>
        public int Interval { get; set; }

        public string Encoding { get; set; }
        public IHTMLAnalyzer HTMLAnalyzer { get; set; }

        //public Dictionary <int,string> InputHTMLDic;


        public Dictionary<int, string> GetSingleInputHTMLDic(int pageIndex )
        {
            //= 0;
            Dictionary<int, string>  inputHTMLDic = new Dictionary<int, string>();
            string inputHTML;
            string url=GetInputURL();

            if (BeforePageGatherHandler != null)
            {
                PageGatherEventAgr arg = new PageGatherEventAgr(url);
                BeforePageGatherHandler(this, arg);
                if (arg.Cancel)
                {
                    return null;
                }
            }
            if (url.Trim().Length==0) 
            {
                return inputHTMLDic;
            }
            inputHTML = CaiyinSoft.NetGather.NetGatherUtility.WebUtils.DoGet(url, System.Text.Encoding.GetEncoding(Encoding));
            inputHTMLDic.Add(pageIndex, inputHTML);

            while (true)
            {
                HTMLAnalyzer.SetInputHTMLUrl(url);
                HTMLAnalyzer.SetInputHTML(inputHTMLDic[pageIndex]);
                SetInputURL( HTMLAnalyzer.NextPageUrl().Trim());

                if (Interval>0)
                        System.Threading.Thread.Sleep(Interval * 1000);

                 break;
            }

            return inputHTMLDic;
        }

        public Dictionary<int, string> GetInputHTMLDic()
        {
            int pageIndex = 0;
            Dictionary<int, string>  inputHTMLDic = new Dictionary<int, string>();
            string inputHTML;

            string url = GetInputURL();
            if (BeforePageGatherHandler != null)
            {
                PageGatherEventAgr arg = new PageGatherEventAgr(url);
                BeforePageGatherHandler(this, arg);
                if (arg.Cancel)
                {
                    return null;
                }
            }
            if (url.Trim().Length == 0)
            {
                return inputHTMLDic;
            }


            inputHTML = CaiyinSoft.NetGather.NetGatherUtility.WebUtils.DoGet(url, System.Text.Encoding.GetEncoding(Encoding));
            inputHTMLDic.Add(pageIndex, inputHTML);

            while (true)
            {
                HTMLAnalyzer.SetInputHTMLUrl(url);
                HTMLAnalyzer.SetInputHTML(inputHTMLDic[pageIndex]);
                url = HTMLAnalyzer.NextPageUrl().Trim();

                if (BeforePageGatherHandler != null)
                {
                    PageGatherEventAgr arg = new PageGatherEventAgr(url);
                    BeforePageGatherHandler(this, arg);
                    if (arg.Cancel)
                    {
                        return null;
                    }
                }

                if (url.Length > 0)
                {
                    if (Interval>0)
                        System.Threading.Thread.Sleep(Interval * 1000);
                    inputHTML = CaiyinSoft.NetGather.NetGatherUtility.WebUtils.DoGet(url, System.Text.Encoding.GetEncoding(Encoding));
                    pageIndex++;
                    inputHTMLDic.Add(pageIndex, inputHTML);
                }
                else
                {
                    break;
                }
            }

            return inputHTMLDic;
        }

        public string GetTestHTML()
        {
            string url = GetInputURL();
            if (url.Trim().Length == 0) return "";
            this.HTMLAnalyzer.SetInputHTMLUrl(url);
            return CaiyinSoft.NetGather.NetGatherUtility.WebUtils.DoGet(url, System.Text.Encoding.GetEncoding(Encoding)); 
        }

        //public List<IHTMLFilter> Convert(string HTML)
        //{
        //    HTMLAnalyzer.SetInputHTML(HTML);
        //    List<IHTMLFilter> result = HTMLAnalyzer.Convert();
        //    return result;

        //}

        public event EventHandler<PageGatherEventAgr> BeforePageGatherHandler;
        //public event EventHandler<PageGatherEventAgr> AfterPageGatherHandler;


    }


    public class PageGatherEventAgr : EventArgs
    {
        public string URL { get;private set; }
        public bool Cancel { get; set; }
        public PageGatherEventAgr(string URL)
        {
            this.URL = URL;
            Cancel = false;
        }
    }


}
